<?php
//测试控制器类
class CatelistController extends Controller{

    private $db;

    public function __init(){
        //判断是否登陆
        $admin = Q('session.admin');
        if(empty($admin)){
            header('location:'.u('User/Login/Login'));
        }
        $this -> assign('admin',$admin);
        //指定函数
        $this -> db = k("Cate");
    }
    //动作方法
    public function index(){
        $catelist = $this -> db -> catelist('');
        //传递参数
        $this -> assign('catelist',$catelist);
        //显示视图
        $this->display('Template/admin/header');
        $this->display('left');
        $this->display();
    }
    //添加和修改类型的页面
    public function addcate(){
        $cid = Q('get.cid','');
        $this -> assign('cid',$cid);
        if(!empty($cid)){
            $where = 'cid ='.$cid;
            $cateinfo = $this -> db -> cateone($where);
            $this -> assign('cateinfo',$cateinfo);
        }
        //显示视图
        $this->display('Template/admin/header');
        $this->display('left');
        $this->display();
    }
    //修改和添加的操作
    public function modify(){
        $cid = Q('get.cid','');

        $Model = K('Cate');
        //表单验证
        if(!$Model -> create()){
            $this -> error($Model -> error);
            exit;
        }
        //添加
        if(empty($cid)){
            $data = array(
                'cname' => $_POST['catename'],
                'pid' => '0',
            );
            if($this -> db -> addcate($data)){
                $this -> success($message = '添加成功。',U('Catelist/index'), $time = 2);
                exit;
            }else{
                $this -> error($Model -> error);
                exit;
            }
        }
        //修改
        if(!empty($cid)){
            $where = 'cid ='.$cid;
            $data = array(
                'cname' => $_POST['catename'],
            );
            if($this -> db -> upcate($where,$data)){
                $this -> success($message = '修改成功。',U('Catelist/index'), $time = 2);
                exit;
            }else{
                $this -> error($Model -> error);
                exit;
            }
        }
    }

    //删除类型
    public function del(){
        $cid = Q('get.cid','');

        if(empty($cid)){
            header('location:'.u('cate/index'));
            exit;
        }
        $where = 'cid = '.$cid;
        if($this -> db -> catedel($where)){
            $this -> success($message = '删除成功。',U('Catelist/index'), $time = 2);
        }else{
            $this -> error('删除失败！');
            exit;
        }
    }
}
